import type { headerForm, shop } from '@/types/shop'
import type { AxiosPromise } from 'axios'
import request from '@/api/request'

/**
 * 获取总览
 * @param data requestType
 * @returns
 */
export function getShopList(data: headerForm): AxiosPromise<any> {
  return request({
    url: '/emrweb/goods/list',
    method: 'post',
    data,
  })
}

/**
 * 批量下架
 * @param data unsaleableType
 */
export function unsaleable(data: unsaleableType[]) {
  return request({
    url: '/emrweb/goods/unsaleable',
    method: 'post',
    data,
  })
}

/**
 * 批量上架
 * @param data unsaleableType
 */
export function saleable(data: unsaleableType[]) {
  return request({
    url: '/emrweb/goods/saleable',
    method: 'post',
    data,
  })
}

/**
 * 往上或者往下一次
 */
export function upOnceSort(data: sortActionItem) {
  return request({
    url: '/emrweb/goods/sort',
    method: 'post',
    data,
  })
}

/**
 * 置顶
 * @param data
 */
export function upTopSort(data: { oid: number }) {
  return request({
    url: '/emrweb/goods/top',
    method: 'post',
    data,
  })
}

/**
 * 商品批量改价
 * @param data
 */
export function goodsModifyPrice(data: {
  number: number
  operator: string
  rounding: Boolean
  spuIds: []
}) {
  return request({
    url: '/emrweb/goods/modifyPrice',
    method: 'post',
    data,
  })
}

/**
 * 删除商品组
 * @param data
 */
export function deleteShops(data: unsaleableType[]) {
  return request({
    url: '/emrweb/goods/delete',
    method: 'post',
    data,
  })
}

/**
 * 批量追加分类
 * @param data
 */
export function addTypes(data: addTypesItem) {
  return request({
    url: '/emrweb/goods/addTypes',
    method: 'post',
    data,
  })
}

/**
 * 加载商品分类
 * @param data
 */
export function getGoodsTypes(data: getTypesItem) {
  return request({
    url: '/emrweb/goodstype/query',
    method: 'post',
    data,
  })
}
/**
 * 加载营销分类
 * @param data
 */
export function getSaleTypes(data: getTypesItem) {
  return request({
    url: '/emrweb/saletype/query',
    method: 'post',
    data,
  })
}

/**
 * 设置营销分类的状态
 * @param data
 */
export function setSaleStatus(data: { id: number }) {
  return request({
    url: '/emrweb/saletype/state',
    method: 'post',
    data,
  })
}

/**
 * 营销分类置顶
 * @param data
 */
export function UpTopSortWithSales(data: { oid: number }) {
  return request({
    url: '/emrweb/saletype/top',
    method: 'post',
    data,
  })
}

/**
 * 营销分类切换排序
 * @param data
 */
export function UpOnceSortWithSales(data: { id: number }) {
  return request({
    url: '/emrweb/saletype/sort',
    method: 'post',
    data,
  })
}

/**
 * 商品分类置顶
 * @param data UpTopSortWithSales
 */
export function UpTopSortWithGoods(data: { oid: number }) {
  return request({
    url: '/emrweb/goodstype/top',
    method: 'post',
    data,
  })
}

/**
 * 商品分类切换排序
 * @param data
 */
export function UpOnceSortWithGooods(data: { id: number }) {
  return request({
    url: '/emrweb/goodstype/sort',
    method: 'post',
    data,
  })
}

/**
 * 删除营销分类
 * @param data
 */
export function deleteSaleType(data: unsaleableType[]) {
  return request({
    url: '/emrweb/saletype/delete',
    method: 'post',
    data,
  })
}

/**
 * 新增或修改营销分类
 * @param data
 */
export function addNewSaleType(data: addCategoryItem) {
  return request({
    url: '/emrweb/saletype/save',
    method: 'post',
    data,
  })
}

/**
 * 修改商品分类状态
 * @param data
 */
export function setGoodStatus(data: { id: number }) {
  return request({
    url: '/emrweb/goodstype/state',
    method: 'post',
    data,
  })
}

/**
 * 删除商品分类
 * @param data
 */
export function deleteShopType(data: unsaleableType[]) {
  return request({
    url: '/emrweb/goodstype/delete',
    method: 'post',
    data,
  })
}
/**
 * 新增或修改商品分类
 * @param data
 */
export function addNewGoodsType(data: addCategoryItem) {
  return request({
    url: '/emrweb/goodstype/save',
    method: 'post',
    data,
  })
}
/**
 * 获取商城订单详情
 * @param id
 */
export function getOrderDetail(id: number) {
  return request({
    url: `/emrweb/xorder/detail/${id}`,
    method: 'get',
  })
}

/**
 * 关闭订单
 * @param data
 */
export function closureOrder(data: { id: number; reason?: string }) {
  return request({
    url: '/emrweb/xorder/close',
    method: 'post',
    data,
  })
}

/**
 * 关闭超时订单
 */
export function closureTimeoutOrder(data: { id: number; reason?: string }) {
  return request({
    url: '/emrweb/xorder/closePaymentTimeout',
    method: 'post',
    data,
  })
}

/**
 * 退款
 */
export function refundWithOrder(data: { id: number; reason?: string }) {
  return request({
    url: '/emrweb/xorder/refund',
    method: 'post',
    data,
  })
}

/**
 * 整单改价 修改一口价
 */
export function modifyPrice(data: any) {
  return request({
    url: '/emrweb/xorder/modifyPrice',
    method: 'POST',
    data,
  })
}

/**
 * 订单商品改价
 */
export function modifyGoodsPrice(data: any) {
  return request({
    url: '/emrweb/xorder/modifyGoodsPrice',
    method: 'POST',
    data,
  })
}
/**
 * 保存商品
 * @param data
 */
export function saveGoods(data: shop) {
  return request({
    url: '/emrweb/goods/save',
    method: 'post',
    data,
  })
}

/**
 * 通过spuid 加载商品详情
 * @param id
 */
export function getGoodsInfoWithSpuId(id: number) {
  return request({
    url: `/emrweb/goods/spu/${id}`,
    method: 'get',
  })
}

/**
 * 保存快递信息
 * @param params
 */
export function editExpressInfo(data: Object) {
  return request({
    url: '/emrweb/xorder/express',
    method: 'post',
    data,
  })
}

/**
 * 获取快递类型
 */
export function getExpressTypes() {
  return request({
    url: `/emrweb/express/getExpressType`,
    method: 'get',
  })
}

/**
 * 获取快递智能识别单号
 */
export function getExpressAutonumber(data: { expressSn: string }) {
  return request({
    url: '/emrweb/express/getExpressAutonumber',
    method: 'get',
    params: data,
  })
}
/**
 * 获取快递物流轨迹
 */
export function queryExpressMessage(data: any) {
  return request({
    url: '/emrweb/express/queryExpressMessage',
    method: 'post',
    data,
  })
}

/**
 * 获取订单
 * @param data
 */
export function getOrderList(data: any) {
  return request({
    url: '/emrweb/xorder/list',
    method: 'post',
    data,
  })
}

/**
 * 获取运费和收货超时配置
 * @param data
 */
export function getPostageInfo(data?: any) {
  return request({
    url: '/emrweb/xconfig/getFreightAndReceiveTimeout',
    method: 'get',
  })
}
/**
 * 获取运费和收货超时配置
 * @param data
 */
export function savePostageInfo(data: any) {
  return request({
    url: '/emrweb/xconfig/saveFreightAndReceiveTimeout',
    method: 'post',
    data,
  })
}
export interface addCategoryItem {
  id?: number
  imgUrl?: string
  name: string
}
// 营销分类的type
export interface getSalesItem extends getTypesItem {
  id: number
}

export interface getTypesItem {
  startTime?: string
  endTime?: string
  name?: string
  pageIndex?: number
  pageSize?: number
  status: number
}

export interface addTypesItem {
  goodsTypeIds: number[]
  saleTypeIds: number[]
  spuIds: number[]
}
export interface unsaleableType {
  itemId: number
  itemName?: string
}

export interface sortActionItem {
  // 发起ID
  oid: number
  // 目标ID
  tid: number
}
